package com.swq.java.code.chapter18.code28;

import java.io.*;

public class User implements Serializable {
    private String name;
    private transient String password;

    public User(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public String toString() {
        return name + " " + password;
    }

    public static void main(String[] args) throws Exception {
        User user = new User("Tom", "123456");
        System.out.println("Before Serialization:" + user);
        ByteArrayOutputStream buf = new ByteArrayOutputStream();

        //把User对象序列化到一个字节缓存中
        ObjectOutputStream o = new ObjectOutputStream(buf);
        o.writeObject(user);

        //从字节缓存中反序列化User对象
        ObjectInputStream in = new ObjectInputStream(
                new ByteArrayInputStream(buf.toByteArray()));
        user = (User) in.readObject();
        System.out.println("After Serialization:" + user);
    }
}